import type { FunctionsInterface, MenuInterface } from '@/api/menu';
import { useMenuStore } from '@/store/menu-store';
import { useMicroAppStore } from '@/store/microapp-store';
import { cloneDeep } from 'lodash-es';

interface useMenuReturnInterface {
  getMenu: () => void;
}

export const useMenu = (): useMenuReturnInterface => {
  const getMenu = async () => {
    const appId = useMicroAppStore().currentApp?.id;
    const menu = useMenuStore().menu;
    if (appId) {
      if (!menu[appId]) {
        const res = await menuApi();
        if (res.ok) {
          if (appId) {
            menu[appId] = cloneDeep(res.data) as MenuInterface;
            menu[appId].shortcutMenu = res.data?.functions.shift()
              ?.children as FunctionsInterface[];
            menu[appId].normalMenu = res.data.functions;
          }
        }
      }
    }
  };
  return {
    getMenu,
  };
};
